package com.vue.power.backend.sys.service;

import com.vue.power.backend.common.base.constant.SystemStaticConst;
import com.vue.power.backend.common.base.dao.BaseDao;
import com.vue.power.backend.common.base.service.BaseService;
import com.vue.power.backend.common.util.date.DateUtil;
import com.vue.power.backend.common.util.encrypt.Md5Util;
import com.vue.power.backend.common.util.json.JsonUtils;
import com.vue.power.backend.common.util.token.TokenUtil;
import com.vue.power.backend.config.redis.RedisCache;
import com.vue.power.backend.sys.dao.UserDao;
import com.vue.power.backend.sys.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional(rollbackFor={IllegalArgumentException.class})
public class UserService extends BaseService<User> {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisCache redisCache;
    // 加密的盐值
    @Value("${base.salt}")
    public String salt;
    // 基础密码
    @Value("${base.password}")
    public String basePassword;

    @Override
    protected BaseDao<User> getDao() {
        return this.userDao;
    }

    @Override
    public boolean save(User entity) throws Exception {
        entity.setPassword(Md5Util.getMd5(basePassword,salt));
        entity.setCrtDate(DateUtil.currentMillitm());
        User user = this.getUser(redisCache);
        entity.setCrtUser(user.getUserName());
        entity.setCrtUserId(user.getUserId());
        return super.save(entity);
    }

    /**
     * 功能描述：根据用户的登录的token来判断用户是否有访问权限
     * @param auth
     * @return
     */
    public Map<String,Object> authByToken(Map<String,Object> auth){
        Map<String,Object> result = new HashMap<>();
        User user = JsonUtils.map2object(auth,User.class);
        user = redisCache.getObject(user.getToken(),User.class);
        if(user!=null){
            redisCache.setObject(user.getToken(),user,3600);
            result.put(SystemStaticConst.RESULT,SystemStaticConst.SUCCESS);
        }else{
            result.put(SystemStaticConst.RESULT,SystemStaticConst.FAIL);
        }
        return result;
    }

    /**
     * 功能描述： 根据用户的token来获取当前用户登录的信息
     * @param user
     * @return
     */
    public  Map<String,Object>  getUserByToken(User user){
        Map<String,Object> result = new HashMap<>();
        user = redisCache.getObject(user.getToken(),User.class);
        if(user!=null){
            result.put(SystemStaticConst.RESULT,SystemStaticConst.SUCCESS);
            result.put("user",user);
            redisCache.setObject(user.getToken(),user,3600);
        }else{
            result.put(SystemStaticConst.RESULT,SystemStaticConst.FAIL);
        }
        return result;
    }

    /**
     * 功能描述：根据账号和密码实现用户的登录功能
     * @param entity
     * @return
     */
    public Map<String,Object> userLogin(User entity){
        Map<String,Object> result = new HashMap<>();
        entity.setPassword(Md5Util.getMd5(entity.getPassword(),salt));
        entity = userDao.userLogin(entity);
        if(entity!=null){
            result.put(SystemStaticConst.RESULT,SystemStaticConst.SUCCESS);
            result.put("token", TokenUtil.getUserToken());
            entity.setToken(result.get("token").toString());
            // 更新用户登录的token和登录时间
            userDao.updateToken(entity);
            redisCache.setObject((String)result.get("token"),entity,3600);
        }else{
            result.put(SystemStaticConst.RESULT,SystemStaticConst.FAIL);
            result.put("token", "");
        }
        return result;
    }
}
